package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.ExceptionLogMapper;
import com.itheima.pojo.ExceptionLog;
import com.itheima.pojo.SysLog;
import com.itheima.service.ExceptionLogService;
import com.itheima.vo.ExceptionLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogMapper exceptionLogMapper;

    @Override
    public Page<ExceptionLog> findLogListPage(ExceptionLogVO exceptionLogVO) {
        if (exceptionLogVO != null) {
            //设置默认当前页
            if (exceptionLogVO.getCurrentPage() == null || "".equals(exceptionLogVO.getCurrentPage())) {
                exceptionLogVO.setCurrentPage(0);
            }
            //设置默认每页条数
            if (exceptionLogVO.getPageSize() == null || "".equals(exceptionLogVO.getPageSize())) {
                exceptionLogVO.setPageSize(10);
            }
        } else {
           exceptionLogVO = new ExceptionLogVO();
           exceptionLogVO.setCurrentPage(0);
           exceptionLogVO.setPageSize(10);
        }
        Integer currentPage = exceptionLogVO.getCurrentPage();
        Integer pageSize = exceptionLogVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example=new Example(SysLog.class);
        Example.Criteria criteria = example.createCriteria();
        if (exceptionLogVO.getStartTime()!=null&&"".equals(exceptionLogVO.getStartTime())){
            String[] time = exceptionLogVO.getStartTime().split(",");
            if (time.length==2){
                criteria.andBetween("createTime",time[0],time[1]);
            }
        }
        return  (Page<ExceptionLog>)exceptionLogMapper.selectByExample(example);
    }
}
